package com.qb.mobile.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qb.mobile.R;
import com.qb.mobile.PhonebookDetail;
import com.qb.mobile.PhonebookList;
import com.qb.mobile.model.Contact;

import android.view.View;
import android.view.View.OnClickListener;

/**
 * @author <a href="mailto:dangcongthanhtrung@gmail.com">dangtrung</a>
 * 
 *         PhonebookAdapter.java class
 * 
 */
public class PhonebookAdapter extends BaseAdapter implements OnClickListener {

	private Context context;
	private List<Contact> listPhonebook;
	
	public PhonebookAdapter(Context context, List<Contact> listPhonebook) {
		this.context = context;
		this.listPhonebook = listPhonebook;
	}

	/**
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		return listPhonebook.size();
	}

	/**
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int position) {
		return listPhonebook.get(position);
	}

	/**
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * @see android.widget.Adapter#getView(int, View, ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		
		Contact entry = listPhonebook.get(position);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.phone_row, null);
		}
		TextView tvContact = (TextView) convertView.findViewById(R.id.txtName);
		tvContact.setText(entry.getName());

		TextView tvPhone = (TextView) convertView.findViewById(R.id.txtMobile);
		tvPhone.setText("Phone : " + entry.getMobilePhone());

		TextView tvAddress = (TextView) convertView.findViewById(R.id.txtAddress);
		tvAddress.setText("Address : " + entry.getAddressHome());

		ImageView image = (ImageView) convertView.findViewById(R.id.image);
		if (entry.getPhoto() != null) {
			byte[] photoBlob = entry.getPhoto();
			Bitmap photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
			Drawable drawable = new BitmapDrawable(photoBitmap);
			image.setBackgroundDrawable(drawable);
		} else {
			image.setBackgroundResource(R.drawable.ic_contact_picture);
		}
		
		convertView.setOnClickListener(this);
		convertView.setTag(entry);
        
		return convertView;
	}

	/**
	 * @see android.view.View.OnClickListener#onClick(View)
	 */
	@Override
	public void onClick(View view) {
		Intent intent = new Intent(context, PhonebookDetail.class);
		Contact entry = (Contact) view.getTag();
		intent.putExtra("Phonebook", entry);
		final int result = 2;
		((PhonebookList) context).startActivityForResult(intent, result);
		
	}

}
